package com.edushare.common.dao.academics.history;

import java.util.List;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.dao.CayenneBasedDAO;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.CurriculumDAO;
import com.edushare.common.dao.EnrolmentsDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.AcademicYearModel;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.UserBatchLink;
import com.edushare.common.vo.Users;

public class BatchEnrol extends CayenneBasedDAO {
	public BatchEnrol(ORMInterface ormInterface) {
		super(ormInterface);
	}
	
	public void userBatchAddTasks(UserBatchLink ubLink){
		if(!ubLink.isStudent())
			return;
		enrolUserInMandatoryCourses(ubLink.getUser(), ubLink.getBatch());
		enrolUserInElectives(ubLink.getUser(), ubLink.getBatch());
	}
	
	public void userUpdateTasks(Users u, Batches b){
		enrolUserInElectives(u, b);
		enrolUserInMandatoryCourses(u, b);
	}

	public void enrolUserInMandatoryCourses(Users u, Batches b){
		int i = 0;
		BatchDAO bDAO = new BatchDAO(ormInterface);
		Curricula curr = b.getCurricula();
		CurriculumDAO cDAO = new CurriculumDAO(ormInterface);
		EnrolmentsDAO eDAO = new EnrolmentsDAO(ormInterface);
		CourseInstancesDAO cInstDAO = new CourseInstancesDAO(ormInterface);
		for(i=0;i<=b.getCurrentSemester();i++){
			List<Courses> manCourses = cDAO.getMandatoryCourses(curr, i);
			for(Courses c:manCourses){
				int year = bDAO.getCalendarYear(b, i);
				AcademicYearModel sem = bDAO.getAcadYearModel(b, i);
				CourseInstances inst = cInstDAO.createInstanceByYearAndSem(c, year, sem);
				Enrolments e = eDAO.addUserWithStatus(inst, u, Enrolments.STUDENT, false);
				e.setBatchLink(b);
				e.setBatchSemNum(i);
			}
		}
		getDataContext().commitChanges();
	}

	public void enrolUserInElectives(Users u, Batches b){
		BatchDAO bDAO = new BatchDAO(ormInterface);
		Curricula curr = b.getCurricula();
		CurriculumDAO cDAO = new CurriculumDAO(ormInterface);
		CourseInstancesDAO cInstDAO = new CourseInstancesDAO(ormInterface);
		EnrolmentsDAO eDAO = new EnrolmentsDAO(ormInterface);
		CourseInstances inst;
		Enrolments e;
		AcademicYearModel semester; 
		for(int sem=0;sem<=b.getCurrentSemester();sem++){
			int numElectiveGroups = cDAO.getNumElectives(curr, sem);
			for(int groupId = 0;groupId < numElectiveGroups;groupId++){
				List<Courses> elCourses = cDAO.getElectiveCourses(curr, sem, groupId);
				for(Courses c:elCourses){
					int year = bDAO.getCalendarYear(b, sem);
					semester = bDAO.getAcadYearModel(b, sem);
					inst = cInstDAO.createInstanceByYearAndSem(c, year, semester);
					e = eDAO.getEnrolment(inst, u);
					if(e!=null && e.getBatchLink()==null){
						e.setBatchLink(b);
						e.setBatchSemNum(sem);
					}
					if(e!=null)
						continue;
				}
			}
		}
		getDataContext().commitChanges();
	}
	
	public UserBatchLink getUBL(Users u, Batches b){
		List<UserBatchLink> ublList = u.getUserBatchLink();
		for(UserBatchLink ubl:ublList){
			if(ubl.getBatch() == b)
				return ubl;
		}
		return null;
	}
}
